package com.yx.front.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.front.config.global.BizException;
import com.yx.front.config.global.CommonResult;
import com.yx.front.enums.CollectEnum;
import com.yx.front.model.BookCollect;
import com.yx.front.model.BookMessage;
import com.yx.front.model.param.BookMessageParam;
import com.yx.front.model.vo.BookMessageVo;
import com.yx.front.model.vo.KindTypeVo;
import com.yx.front.service.BookCollectService;
import com.yx.front.service.BookMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.yx.front.converter.BookMessageConverter.getBookMessageVo;
import static com.yx.front.converter.BookMessageConverter.handleResult;

/**
 * @author YangXiong
 */
@Component
public class BookMessageManager {
    @Resource
    private BookMessageService bookMessageService;
    @Resource
    private BookCollectService bookCollectService;

    public BookMessageVo getDetail(Long id) {
        if (Objects.isNull(id)) {
            throw new BizException("参数不能为空");
        }
        BookMessage bookMessage = bookMessageService.getById(id);
        BookMessageVo bookMessageVo = getBookMessageVo(bookMessage);
        LambdaQueryWrapper<BookCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookCollect::getBookId, id);
        BookCollect one = bookCollectService.getOne(wrapper);
        if(Objects.isNull(one)){
            bookMessageVo.setIsCollect(CollectEnum.NO.getValue());
            bookMessageVo.setCollected(CollectEnum.NO.getCode());
            return bookMessageVo;
        }
        bookMessageVo.setIsCollect(CollectEnum.YES.getValue());
        bookMessageVo.setCollected(CollectEnum.YES.getCode());
        return bookMessageVo;
    }

    public CommonResult<List<BookMessageVo>> getBookMessageList(BookMessageParam param) {
        //动态参数 筛选条件
        LambdaQueryWrapper<BookMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getKind()), BookMessage::getKind, param.getKind());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(param.getName()), BookMessage::getName, param.getName());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getType()), BookMessage::getType, param.getType());
        Page<BookMessage> bookMessagePage = new Page<>(param.getCurrentPage(), param.getTotalPage());
        Page<BookMessage> page = bookMessageService.page(bookMessagePage, lambdaQueryWrapper);
        List<BookMessageVo> result = handleResult(page.getRecords());
        return CommonResult.success(result, page.getTotal());
    }

    public CommonResult<KindTypeVo> getTypeAndKind() {
        List<BookMessage> list = bookMessageService.list();
        Set<String> kind = new HashSet<>();
        Set<String> type = new HashSet<>();
        for (BookMessage message : list) {
            kind.add(message.getKind());
            type.add(message.getType());
        }
        KindTypeVo typeVo = new KindTypeVo();
        typeVo.getKind().add("全部");
        typeVo.getType().add("全部");
        typeVo.getKind().addAll(new ArrayList<>(kind));
        typeVo.getType().addAll(new ArrayList<>(type));
        return CommonResult.success(typeVo);
    }

    public CommonResult<List<BookMessageVo>> adviceList() {
        List<Long> ids = bookMessageService.list().stream().map(BookMessage::getId).collect(Collectors.toList());
        Collections.shuffle(ids);
        List<Long> subList = ids.subList(0, 8);
        List<BookMessage> bookMessages = bookMessageService.listByIds(subList);
        List<BookMessageVo> result = handleResult(bookMessages);
        return CommonResult.success(result);
    }
}
